package com.xiaoq.system.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoq.apis.sites.IZoneMasterApi;
import com.xiaoq.apis.sites.ZoneMaster4DataStatisVo;
import com.xiaoq.sites.entity.SitesEntity;
import com.xiaoq.sites.service.ISitesService;
import com.xiaoq.system.entity.ZoneMasterEntity;
import com.xiaoq.system.service.IZoneMasterService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenjh on 2017/12/26.
 */
@Service(interfaceClass=IZoneMasterApi.class, version="1.0.0")
public class ZoneMasterApiImpl implements IZoneMasterApi {

    @Autowired
    private IZoneMasterService zoneMasterService;

    @Autowired
    private ISitesService sitesService;

    @Override
    public String getZoneNameBySitesId(String sitesId) {
        if(StringUtils.isBlank(sitesId)){
            return null;
        }

        SitesEntity sitesEntity = sitesService.findById(sitesId);
        if(sitesEntity==null || StringUtils.isBlank(sitesEntity.getZoneCity())){
            return null;
        }

        ZoneMasterEntity zoneMasterEntity = zoneMasterService.getZoneMasterByCode(sitesEntity.getZoneCity());
        if(zoneMasterEntity != null){
            return zoneMasterEntity.getName();
        }
        return null;
    }

    @Override
    public List<ZoneMaster4DataStatisVo> getZoneMasterListByAreaCode(List<String> areaCodeList) {
        List<ZoneMaster4DataStatisVo> resultList = new ArrayList<>();
        if(CollectionUtils.isEmpty(areaCodeList)){
            return resultList;
        }

        List<ZoneMasterEntity> zoneMasterList = zoneMasterService.findAllZoneMasterList(areaCodeList, 1);
        ZoneMaster4DataStatisVo zoneMaster4DataStatisVo;
        if(CollectionUtils.isNotEmpty(zoneMasterList)){
            for (ZoneMasterEntity zoneMasterEntity : zoneMasterList){
                zoneMaster4DataStatisVo = new ZoneMaster4DataStatisVo();

                zoneMaster4DataStatisVo.setAreaCode(zoneMasterEntity.getAreaCode());
                zoneMaster4DataStatisVo.setCode(zoneMasterEntity.getCode());
                zoneMaster4DataStatisVo.setId(zoneMasterEntity.getId());
                zoneMaster4DataStatisVo.setLatitude(zoneMasterEntity.getLatitude());
                zoneMaster4DataStatisVo.setLongitude(zoneMasterEntity.getLongitude());
                zoneMaster4DataStatisVo.setName(zoneMasterEntity.getName());
                zoneMaster4DataStatisVo.setLevels(zoneMasterEntity.getLevels());

                resultList.add(zoneMaster4DataStatisVo);
            }
        }

        return resultList;
    }
}
